<?php

class FBMethod{
	
	public function __construct() {
		
	}
	
	public function getPosts($beauty_posts,$fb_base_url,$page_id,$page_until=false){
		
		$ch = curl_init();
		
		if(!$page_until){			
			$params = $page_id.'/photos';        
			$params .= '?limit=32';
			$params .= '&access_token='.$this->getAccessToken('lilindead@gmail.com','137261822975544');					
			echo $fb_base_url.$params;
			curl_setopt($ch, CURLOPT_URL, $fb_base_url.$params);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			//curl_setopt($ch, CURLOPT_HEADER, 1);
			$result = curl_exec($ch);
			//$headers = curl_getinfo($ch);
			curl_close($ch);
			
			$result_decoded = json_decode($result, true);
			echo "<pre>";print_r($result);echo "</pre>";
			echo "<pre>";print_r($result_decoded);echo "</pre>";exit;
			$result_decoded['next_id'] = $page_id;
		}
		else{			
			$next_page = $fb_base_url.$page_id.'/photos?limit=32&until='.$page_until;	
			//echo  $next_page;		
			curl_setopt($ch, CURLOPT_URL, $next_page);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			//curl_setopt($ch, CURLOPT_HEADER, 0);
			$result = curl_exec($ch);
			//$headers = curl_getinfo($ch);
			curl_close($ch);
			$result_decoded = json_decode($result, true);	
			$result_decoded['next_id'] = $page_id;		
		}
		
		if(empty($result_decoded['data'])){
			$page_id = $this->findNextID($beauty_posts,$page_id);
			if($page_id){
				return $this->getPosts($beauty_posts,$fb_base_url,$page_id);
			}
			else{
				return false;
			}
		}
		
		return $result_decoded;
	}
	
	private function findNextID($beauty_posts,$current_page_id){	
	//echo "<pre>";print_r($beauty_posts);echo "</pre>";	
		foreach($beauty_posts as $post_key=>$post_detail){			
			if($post_detail['id'] == $current_page_id){
				if(isset($beauty_posts[($post_key+1)]['id'])){
					$page_id = $beauty_posts[($post_key+1)]['id'];
					return $page_id;
				}
				else return false;
			}
		}
	}
	
	public function getLikes($fb_base_url,$like_cache_path){
		$IDs = $this->getLikesID($like_cache_path);
		
		$ch = curl_init();
		$params = '?ids='.$IDs;        
		
		curl_setopt($ch, CURLOPT_URL, $fb_base_url.$params);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		$result = curl_exec($ch);
		$headers = curl_getinfo($ch);
		curl_close($ch);
		$result_decoded = json_decode($result, true);
		return $result_decoded;
	}
	
	public function getAccessToken($username,$app_id){
		  //$username = "lilindead@gmail.com";
		  $password = "weilin730519";
		 
		  // access to facebook home page (to get the cookies)
		  $curl = curl_init();
		  curl_setopt($curl, CURLOPT_URL, "http://www.facebook.com");
		  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
		  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		  curl_setopt($curl, CURLOPT_ENCODING, "");
		  curl_setopt($curl, CURLOPT_COOKIEJAR, getcwd() . '/cookies_facebook.cookie');
		  curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
		  $curlData = curl_exec($curl);
		  curl_close($curl);
		 
		  // do get some parameters for login to facebook
		  $charsetTest = substr($curlData, strpos($curlData, "name=\"charset_test\""));
		  $charsetTest = substr($charsetTest, strpos($charsetTest, "value=") + 7);
		  $charsetTest = substr($charsetTest, 0, strpos($charsetTest, "\""));
		 
		  $locale = substr($curlData, strpos($curlData, "name=\"locale\""));
		  $locale = substr($locale, strpos($locale, "value=") + 7);
		  $locale = substr($locale, 0, strpos($locale, "\""));
		 
		  $lsd = substr($curlData, strpos($curlData, "name=\"locale\""));
		  $lsd = substr($lsd, strpos($lsd, "value=") + 7);
		  $lsd = substr($lsd, 0, strpos($lsd, "\""));
		 
		  // do login to facebook
		  $curl = curl_init();
		  curl_setopt($curl, CURLOPT_URL, "https://login.facebook.com/login.php?login_attempt=1");
		  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
		  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		  curl_setopt($curl, CURLOPT_POST, 1);
		  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		  curl_setopt($curl, CURLOPT_POSTFIELDS, "charset_test=" . $charsetTest . "&locale=" . $locale . "&non_com_login=&email=" . $username . "&pass=" . $password . "&charset_test=" . $charsetTest . "&lsd=" . $lsd);
		  curl_setopt($curl, CURLOPT_ENCODING, "");
		  curl_setopt($curl, CURLOPT_COOKIEFILE, getcwd() . '/cookies_facebook.cookie');
		  curl_setopt($curl, CURLOPT_COOKIEJAR, getcwd() . '/cookies_facebook.cookie');
		  curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
		  $curlData = curl_exec($curl);
		  curl_close($curl);
		 
		 
		 $curl = curl_init();
		  curl_setopt($curl, CURLOPT_URL, "https://graph.facebook.com/oauth/authorize?client_id=".$app_id."&=http://www.facebook.com/connect/login_success.html&type=user_agent&display=popup&redirect_uri=http://www.esearches.com.tw/public/");
		  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
		  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		  curl_setopt($curl, CURLOPT_COOKIEFILE, getcwd() . '/cookies_facebook.cookie');
		  curl_setopt($curl, CURLOPT_COOKIEJAR, getcwd() . '/cookies_facebook.cookie');
		  curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
		  curl_setopt($curl, CURLOPT_HEADER, 1);
		  
		  $headers = curl_getinfo($curl);
		
		  $curlData = curl_exec($curl);
		  curl_close($curl);
		  $access_token = substr($curlData, strpos($curlData, "access_token=") + 13);
		  $access_token = substr($access_token, 0, strpos($access_token, "&"));
		  echo $access_token;
		  return $access_token;
	}
	
	private function getLikesID($like_cache_path){
		$likes = unserialize(file_get_contents($like_cache_path));
		$IDs = '';
		foreach($likes as $key=>$like_term){
			$IDs .= $like_term['id'].',';
		}
		
		return substr($IDs, 0, -1);
	}
}

?>